package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.gulimall.product.vo.SpuSaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.SpuInfoEntity;
import com.atguigu.gulimall.product.service.SpuInfoService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    /*查询*/

    /**
     * http://localhost:88/api/product/spuinfo/list?t=1681562567347&page=1&limit=10    spu检索
     * 根据开发文档传来的参数有:key  catelogId  brandId  status
     * 响应的数据有:
     * 分页参数  "list": [{
     * <p>
     * "brandId": 0, //品牌id
     * "brandName": "品牌名字",
     * "catalogId": 0, //分类id
     * "catalogName": "分类名字",
     * "createTime": "2019-11-13T16:07:32.877Z", //创建时间
     * "id": 0, //商品id
     * "publishStatus": 0, //发布状态
     * "spuDescription": "string", //商品描述
     * "spuName": "string", //商品名字
     * "updateTime": "2019-11-13T16:07:32.877Z", //更新时间
     * "weight": 0 //重量
     * }]
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }
    /*增*/

    /**
     * 新增商品
     * /product/spuinfo/save
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVO vo) {
        /**
         * 我们要保存spu,sku,images,中间表等各种相关信息还要跨系统去gulimall_sms里面，
         * 保存pms_sku_full_reduction积分,sms_sku_ladder满减,sms_spu_bounds,spu的积分都要保存
         * 大保存
         */
        spuInfoService.saveSpuInfo(vo);

        return R.ok();
    }
    /*删*/

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**/

    /**
     * 改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 商品上架功能   POST:/product/spuinfo/{spuId}/up
     * 请求的参数:spuId
     * 响应的数据
     * {
     * "msg": "success",
     * "code": 0
     * }
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId) {
        spuInfoService.up(spuId);
        return R.ok();
    }
}
